package com.yuqing.sentimentapp.workspace.todayreport;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.todayreport.entity.TodayReportInfoResult;
import com.yuqing.sentimentapp.workspace.todayreport.entity.TodayReportReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 台风抢险
 * Created by shenhe on 2020/4/3.
 */
public class TodayReportFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.et_content)
    EditText mEtContent;
    @BindView(R.id.et_workstatus)
    EditText mEtWorkstatus;
    @BindView(R.id.et_festival)
    EditText mEtFestival;
    @BindView(R.id.et_reportor)
    EditText mEtReportor;
    @BindView(R.id.et_auditor)
    EditText mEtAuditor;


    private boolean editEnable = true;
    private TodayReportInfoResult.DataBean actionInfo;
    /**
     * 从已上报列表跳转过来的
     */
    private TodayReportReportResult.DataBean.ReportsBean reportsInfo;


    public static void goActivity(Activity activity, TodayReportInfoResult.DataBean actionInfo) {
        Intent intent = new Intent(activity, TodayReportFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, TodayReportReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, TodayReportFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_today_report_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((TodayReportInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        reportsInfo = ((TodayReportReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));

    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtContent.setText(actionInfo.getContent());
            mEtWorkstatus.setText(actionInfo.getWorkstatus());
            mEtFestival.setText(actionInfo.getFestival());
            mEtReportor.setText(actionInfo.getCreator());
            mEtAuditor.setText(actionInfo.getAuditor());
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtContent.setText(reportsInfo.getContent());
            mEtWorkstatus.setText(reportsInfo.getWorkstatus());
            mEtFestival.setText(reportsInfo.getFestival());
            mEtReportor.setText(reportsInfo.getCreator());
            mEtAuditor.setText(reportsInfo.getAuditor());

            mEtContent.setEnabled(false);
            mEtWorkstatus.setEnabled(false);
            mEtFestival.setEnabled(false);
            mEtReportor.setEnabled(false);
            mEtAuditor.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (TextUtils.isEmpty(mEtContent.getText().toString())) {
                    showShortToast("请输入消防要情");
                    return;
                }
                if (TextUtils.isEmpty(mEtWorkstatus.getText().toString())) {
                    showShortToast("请输入工作动态");
                    return;
                }
                if (TextUtils.isEmpty(mEtFestival.getText().toString())) {
                    showShortToast("请输入节日（安保）综合情况");
                    return;
                }
                if (TextUtils.isEmpty(mEtReportor.getText().toString())) {
                    showShortToast("请输入填报人");
                    return;
                }
                if (TextUtils.isEmpty(mEtAuditor.getText().toString())) {
                    showShortToast("请输入审核人");
                    return;
                }
                if (actionInfo == null) {
                    actionInfo = new TodayReportInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setContent(mEtContent.getText().toString());
        actionInfo.setWorkstatus(mEtWorkstatus.getText().toString());
        actionInfo.setFestival(mEtFestival.getText().toString());
        actionInfo.setCreator(mEtReportor.getText().toString());
        actionInfo.setAuditor(mEtAuditor.getText().toString());
    }

    private void updateReportInfo(TodayReportInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateTodayReportReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(TodayReportInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveTodayReportReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
